#include <Mesh/Mesh.h>

/********************************/
/*			Public
/********************************/

    /********************************
    /*			Construcors
    /********************************/
			
        Mesh::Mesh(){
            Meshes.push_back(this);
        }

        Mesh::Mesh(VertexTemplate *vt){
            m_VertexTemplate    =   vt;
            Meshes.push_back(this);
        }
			
    /********************************
    /*			Methods
    /********************************/

        void Mesh::Display(){
            glDrawElements(GL_TRIANGLES, m_Triangles.size()*3, GL_UNSIGNED_SHORT, (void *)+0);
        }

        void Mesh::NewTriangle(Vertex&  v1, Vertex & v2, Vertex & v3){

            Triangle * triangle = new Triangle(m_Triangles.size()*3);
            triangle->SetVertices(&v1,&v2,&v3);
            m_Triangles.push_back(triangle);
        }

        void Mesh::RemoveTriangle(int pos){
            Triangle * triangle = m_Triangles.at(pos);
            triangle->DeleteTriangle();
            m_Triangles.erase(m_Triangles.begin()+ pos);
        }


         /********************************
         /*			Get
         /********************************/

            VertexTemplate &    Mesh::GetVertexTemplate(){
               return *m_VertexTemplate;
            }

         /********************************
         /*			Set
         /********************************/

         /********************************
         /*			Static
         /********************************/

            Mesh &   Mesh::Rect2D(float width, float height, VertexTemplate & vertexTemplate, int position,
                                 int color, int normal, int texCoord){

                Mesh * mesh =   new Mesh(&vertexTemplate);


                Vertex v1 = vertexTemplate.NewVertex();
                v1.SetAttribute(position, -width, -height, -0.0, 1.0);
                v1.SetAttribute(color, 1.0, 0.0, 0.0, 1.0);
                v1.SetAttribute(normal, 1.0, 0.0, 0.0, 1.0);
                v1.SetAttribute(texCoord,0.0, 1.0, 0.0, 1.0);

                Vertex v2 = vertexTemplate.NewVertex();
                v2.SetAttribute(position, width, -height, -0.0, 1.0);
                v2.SetAttribute(color, 0.0, 1.0, 0.0, 1.0);
                v2.SetAttribute(normal, 0.0, 0.0, 0.0, 1.0);
                v2.SetAttribute(texCoord,1.0, 1.0, 1.0, 1.0);

                Vertex v3 = vertexTemplate.NewVertex();
                v3.SetAttribute(position, width, height, -0.0, 1.0);
                v3.SetAttribute(color, 0.0, 0.0, 1.0, 1.0);
                v3.SetAttribute(normal, 0.0, 0.0, 0.0, 1.0);
                v3.SetAttribute(texCoord,1.0, 0.0, 1.0, 1.0);

                Vertex v4 = vertexTemplate.NewVertex();
                v4.SetAttribute(position, -width, height, -0.0, 1.0);
                v4.SetAttribute(color, 0.0, 0.0, 1.0, 1.0);
                v4.SetAttribute(normal, 0.0, 0.0, 0.0, 1.0);
                v4.SetAttribute(texCoord,0.0, 0.0, 0.0, 1.0);

                mesh->NewTriangle(v3,v2,v1);
                mesh->NewTriangle(v1,v3,v4);

                return *mesh;
            }

            Mesh &   Mesh::Rect3D(float width, float height,float depth, VertexTemplate & vertexTemplate, int position,
                                 int color, int normal, int texCoord){

                Mesh * mesh =   new Mesh(&vertexTemplate);

                Vertex v1 = vertexTemplate.NewVertex();
                v1.SetAttribute(position, -width, -height, depth, 1.0);
                v1.SetAttribute(color, 1.0, 0.0, 0.0, 1.0);
                v1.SetAttribute(normal, 1.0, 0.0, 0.0, 1.0);
                v1.SetAttribute(texCoord,0.0, 1.0, 0.0, 1.0);

                Vertex v2 = vertexTemplate.NewVertex();
                v2.SetAttribute(position, width, -height, depth, 1.0);
                v2.SetAttribute(color, 0.0, 1.0, 0.0, 1.0);
                v2.SetAttribute(normal, 0.0, 0.0, 0.0, 1.0);
                v2.SetAttribute(texCoord,1.0, 1.0, 1.0, 1.0);

                Vertex v3 = vertexTemplate.NewVertex();
                v3.SetAttribute(position, width, height, depth, 1.0);
                v3.SetAttribute(color, 0.0, 0.0, 1.0, 1.0);
                v3.SetAttribute(normal, 0.0, 0.0, 0.0, 1.0);
                v3.SetAttribute(texCoord,1.0, 0.0, 1.0, 1.0);

                Vertex v4 = vertexTemplate.NewVertex();
                v4.SetAttribute(position, -width, height, depth, 1.0);
                v4.SetAttribute(color, 0.0, 0.0, 1.0, 1.0);
                v4.SetAttribute(normal, 0.0, 0.0, 0.0, 1.0);
                v4.SetAttribute(texCoord,0.0, 0.0, 0.0, 1.0);


                Vertex v5 = vertexTemplate.NewVertex();
                v5.SetAttribute(position, -width, -height, -depth, 1.0);
                v5.SetAttribute(color, 1.0, 0.0, 0.0, 1.0);
                v5.SetAttribute(normal, 1.0, 0.0, 0.0, 1.0);
                v5.SetAttribute(texCoord,0.0, 1.0, 1.0, 2.0);

                Vertex v6 = vertexTemplate.NewVertex();
                v6.SetAttribute(position, width, -height, -depth, 1.0);
                v6.SetAttribute(color, 0.0, 1.0, 0.0, 1.0);
                v6.SetAttribute(normal, 0.0, 0.0, 0.0, 1.0);
                v6.SetAttribute(texCoord,1.0, 1.0, 2.0, 2.0);

                Vertex v7 = vertexTemplate.NewVertex();
                v7.SetAttribute(position, width, height, -depth, 1.0);
                v7.SetAttribute(color, 0.0, 0.0, 1.0, 1.0);
                v7.SetAttribute(normal, 0.0, 0.0, 0.0, 1.0);
                v7.SetAttribute(texCoord,1.0, 0.0, 2.0, 2.0);

                Vertex v8 = vertexTemplate.NewVertex();
                v8.SetAttribute(position, -width, height, -depth, 1.0);
                v8.SetAttribute(color, 0.0, 0.0, 1.0, 1.0);
                v8.SetAttribute(normal, 0.0, 0.0, 0.0, 1.0);
                v8.SetAttribute(texCoord,0.0, 0.0, 1.0, 2.0);

                // Face avant
                mesh->NewTriangle(v3,v2,v1);
                mesh->NewTriangle(v1,v3,v4);

                //Face Arriere
                mesh->NewTriangle(v7,v5,v6);
                mesh->NewTriangle(v5,v7,v8);

                //Face gauche
                mesh->NewTriangle(v5,v4,v1);
                mesh->NewTriangle(v5,v4,v8);

                //Face Droite
                mesh->NewTriangle(v2,v3,v6);
                mesh->NewTriangle(v6,v7,v3);

                //Face Dessus
                mesh->NewTriangle(v4,v3,v7);
                mesh->NewTriangle(v4,v7,v8);

                //Face Dessous
                mesh->NewTriangle(v1,v2,v6);
                mesh->NewTriangle(v1,v6,v5);

                return *mesh;
            }

    /********************************
    /*			Properties
    /********************************/

        /********************************
        /*			Static
        /********************************/

             vector<Mesh*>    Mesh::Meshes;
				

 /********************************/
 /*			Private
 /********************************/

    /********************************/
    /*			Methods
    /********************************/











